window.onload = function() {


    //用户窗口高度
    var UserclientHeight = document.documentElement.clientHeight;

    //背景图片
    var imgs = document.getElementById('imgbg');
    //存储cookie
    var storage = window.localStorage;


    //默认存储
    if (storage['bgPreference'] == null && storage['a'] == null) {
        storage['a'] = 1;
        // storage['bgPreference'] = 'file:///F:/[%E5%89%8D%E7%AB%AF]%E4%BB%A3%E7%A0%81%E5%BA%93/%E5%B0%8F%E6%A1%88%E4%BE%8B/images/3aa3b1a332986b5744d7bbb4a82cab443cd71a1c.jpg';
        storage['bgPreference'] = '';
    }
    if (storage.a == 1) {
        imgs.style.background = 'url(' + storage.bgPreference + ') no-repeat';
        imgs.style.backgroundSize = '100% ' + UserclientHeight + 'px';
        imgs.style.backgroundPositionX = 'center';
        imgs.style.backgroundPositionY = '0';
        imgs.style.opacity = 1;
        videobg.style.opacity = 0;
    }

    if (storage.a == 2) {
        videobg.src = storage.bgPreference;
        imgs.style.opacity = 0;
        videobg.style.opacity = 1;
    }


    //选择图片更改背景
    var bjimg = document.getElementsByClassName('bjimg');



    var bjvideo = document.getElementsByClassName('bjvideo');

    for (var i = 0; i < bjvideo.length; i++) {
        bjvideo[i].onclick = function() {
            for (var j = 0; j < bjvideo.length; j++) {
                if (this == bjvideo[j]) {
                    storage['bgPreference'] = this.src;
                    //2代表动态
                    storage['a'] = 2;
                }
            }
            videobg.src = this.src;
            imgs.style.opacity = 0;
            videobg.style.opacity = 1;
        }
    }



    for (var i = 0; i < bjimg.length; i++) {
        bjimg[i].onclick = function() {
            imgs.style.background = 'url(' + this.src + ') no-repeat';
            for (var j = 0; j < bjimg.length; j++) {
                if (this == bjimg[j]) {
                    storage['bgPreference'] = this.src;
                    //1代表普通壁纸
                    storage['a'] = 1;

                }
            }
            imgs.style.backgroundSize = '100% ' + UserclientHeight + 'px';
            imgs.style.backgroundPositionX = 'center';
            imgs.style.backgroundPositionY = '0';
            imgs.style.opacity = 1;
            videobg.style.opacity = 0;
        }

    }





    //设置按钮
    var setting = document.getElementById('setting');

    //设置菜单
    var setmenu = document.getElementById('setmenu');
    //设置菜单的背景设置
    var imgsettingLi = document.getElementById('imgsettingLi');

    var colse = document.getElementById('colse');

    //背景设置页面
    var imgsetting = document.getElementById('imgsetting');


    //关闭关于我
    var colseAboutMe = document.getElementById('colseAboutMe');
    //关于我页面
    var aboutMe = document.getElementById('aboutMe');
    //关于我按钮
    var menuAboutMe = document.getElementById('menuAboutMe');

    menuAboutMe.onclick = function() {
        //壁纸
        imgbg.style.filter = 'blur(1.3px)';
        imgbg.style.transform = 'scale(1.1)';

        //动态壁纸
        videobg.style.filter = 'blur(1.3px)';
        videobg.style.transform = 'scale(1.1)';
        aboutMe.style.zIndex = '9999';
        aboutMe.style.display = 'block';
    }

    colseAboutMe.onclick = function() {
        //壁纸
        imgbg.style.filter = 'blur(0px)';
        imgbg.style.transform = 'scale(1)';
        //动态壁纸
        videobg.style.filter = 'blur(0px)';
        videobg.style.transform = 'scale(1)';
        aboutMe.style.zIndex = '0';
        aboutMe.style.display = 'none';
    }

    //关闭背景设置页面
    colse.onclick = function() {
        //壁纸
        imgbg.style.filter = 'blur(0px)';
        imgbg.style.transform = 'scale(1)';
        //动态壁纸
        videobg.style.filter = 'blur(0px)';
        videobg.style.transform = 'scale(1)';
        imgsetting.style.zIndex = '0';
        imgsetting.style.display = 'none';

    }

    imgsettingLi.onclick = function() {
        //壁纸
        imgbg.style.filter = 'blur(1.3px)';
        imgbg.style.transform = 'scale(1.1)';

        //动态壁纸
        videobg.style.filter = 'blur(1.3px)';
        videobg.style.transform = 'scale(1.1)';
        imgsetting.style.zIndex = '9999';
        imgsetting.style.display = 'block';

    }

    //0关闭 1打开
    setting.onmouseenter = function() {
        setmenu.style.zIndex = 9999;
        setmenu.style.display = 'block';
    }
    setmenu.onmouseleave = function() {
        setmenu.style.zIndex = 0;
        setmenu.style.display = 'none';
    }

    /*
    点击关闭开启功能
    var menufalg = 1;
    setting.onclick = function() {
        if (menufalg == 1) {
            setmenu.style.zIndex = 999;
            menufalg = 0;
            return;
        }
        if (menufalg == 0) {
            menufalg = 1;
            setmenu.style.zIndex = 0;
            return;
        }
    } */


}




var inputSearch = document.getElementById('inputSearch');
//获取结果集对象
var li = document.getElementsByClassName('resultSearchLi');

for (var i = 0; i < li.length; i++) {
    li[i].addEventListener('click', function() {
        for (var i = 0; i < li.length; i++) {
            if (this == li[i]) {
                if (i == 0) {
                    location.href = 'https://fanyi.baidu.com/#en/zh/' + li[i].innerHTML.substring(3, li[i].innerHTML.length);
                }
                location.href = 'https://www.baidu.com/s?wd=' + li[i].innerHTML;
            }

        }
    })
}




inputSearch.addEventListener('keydown', function(e) {
    var strLength = inputSearch.value.length + 1;
    var aaaa = 1;
    if (e.keyCode == 8 && strLength == 1) {
        console.log(aaaa);
        aaaa = aaaa - 2;
    }

    if (strLength > 0) {
        window.removeEventListener('keydown', windowEventListener);
        ptag[0].style.visibility = 'hidden';
        ptag[1].style.visibility = 'hidden';
        tag.style.visibility = 'hidden';
        for (var i = 0; i < li.length; i++) {
            li[i].style.display = 'block';
        }
    }
    if (aaaa < 0) {
        for (var i = 0; i < li.length; i++) {
            li[i].style.display = 'none';
        }
    }


})

var windowEventListener = function(e) {
    if (e.keyCode) {
        ptag[0].style.visibility = 'visible';
        imgbg.style.filter = 'blur(1.3px)';
        imgbg.style.transform = 'scale(1.1)';
        cover.style.opacity = 1;
        inputSearch.style.width = '500px';
        inputSearch.style.color = 'rgba(0, 0, 0, 0.8)';
        inputSearch.removeEventListener('mouseleave', aa);
        inputSearch.focus();
    }
    if (e.keyCode == 13) {
        location.href = 'https://www.baidu.com/s?wd=' + inputSearch.value; // s代表搜索，wd代表word
    }
}
window.addEventListener('keydown', windowEventListener);




//禁止打开菜单鼠标事件
document.addEventListener('contextmenu', function(e) {
    e.preventDefault();

})


var time = document.getElementById('time');

var inputSearch = document.getElementById('inputSearch');
var imgbg = document.getElementById('imgbg');

var videobg = document.getElementById('videobg');

var cover = document.getElementById('cover');
var tag = document.getElementById('tag');
var ptag = document.getElementsByClassName('ptag');
var navboxCustom = document.getElementById('navboxCustom');

//1代表打开链接列表、0表示打开搜索
var timeFlag = 1;
time.addEventListener('click', function() {
    //打开链接列表
    if (timeFlag == 1) {
        navboxCustom.style.display = 'block';
        imgbg.style.filter = 'blur(1.3px)';
        imgbg.style.transform = 'scale(1.1)';
        inputSearch.style.display = 'none';
        timeFlag = 0;
        return;
    }
    //打开搜索引擎
    if (timeFlag == 0) {
        navboxCustom.style.display = 'none';
        imgbg.style.filter = 'blur(0px)';
        imgbg.style.transform = 'scale(1)';
        inputSearch.style.display = 'inline-block';
        timeFlag = 1;
        return;
    }
})


inputSearch.value = 'Search';

inputSearch.addEventListener('mouseenter', function() {
    this.style.width = '500px';
    this.style.color = 'rgba(0, 0, 0, 0.8)';
})

var aa = function() {
    this.style.width = '250px';
    this.style.color = ' rgba(255, 255, 255, 0.9)';
}
inputSearch.addEventListener('mouseleave', aa);

tag.addEventListener('mouseout', function() {
    inputSearch.focus();
    ptag[0].style.visibility = 'visible';
    imgbg.style.filter = 'blur(1.3px)';
    imgbg.style.transform = 'scale(1.1)';

    cover.style.opacity = 1;
    inputSearch.value = '';
    inputSearch.style.width = '500px';
    inputSearch.style.color = 'rgba(0, 0, 0, 0.8)';
    tag.style.background = 'rgba(255, 255, 255, 0.25)';
})

//申明全局变量
var timeStart, timeEnd, mouseDownSetInterval;

//获取此刻时间
function getTimeNow() {
    var now = new Date();
    return now.getTime();
}
tag.addEventListener('mousedown', function() {
    //获取鼠标按下时的时间
    timeStart = getTimeNow();
    //setInterval会每100毫秒执行一次，也就是每100毫秒获取一次时间
    mouseDownSetInterval = setInterval(function() {
        timeEnd = getTimeNow();
        //字体变红
        tag.style.background = 'linear-gradient(90deg, green, #CD5C5C, pink, #1E90FF)';
        //如果此时检测到的时间与第一次获取的时间差有1000毫秒
        if (timeEnd - timeStart > 1000) {
            console.log(timeEnd);
            console.log(timeStart);
            //便不再继续重复此函数 （clearInterval取消周期性执行）
            clearInterval(mouseDownSetInterval);
        }
    }, 100);

})

tag.addEventListener('mouseup', function() {
    clearInterval(mouseDownSetInterval);
    tag.style.background = 'none';
    tag.style.background = 'rgba(255, 255, 255, .25)';
})

tag.addEventListener('mouseleave', function() {
    ptag[1].style.visibility = 'hidden';
    tag.style.visibility = 'hidden';
});

tag.addEventListener('mouseenter', function() {
    ptag[1].style.visibility = 'visible';
    tag.style.visibility = 'visible';


});

//获取焦点
inputSearch.addEventListener('focus', function() {
    ptag[0].style.visibility = 'visible';

    //壁纸
    imgbg.style.filter = 'blur(1.3px)';
    imgbg.style.transform = 'scale(1.1)';

    //动态壁纸
    videobg.style.filter = 'blur(1.3px)';
    videobg.style.transform = 'scale(1.1)';
    cover.style.opacity = 1;
    inputSearch.value = '';
    this.style.width = '500px';
    this.style.color = 'rgba(0, 0, 0, 0.8)';
    inputSearch.removeEventListener('mouseleave', aa);
})



//失去焦点
var onblurs = function() {
    for (var i = 0; i < li.length; i++) {
        li[i].style.display = 'none';
    }
    ptag[0].style.visibility = 'hidden';
    ptag[1].style.visibility = 'hidden';
    this.style = '';
    tag.style.visibility = 'hidden';
    this.style.width = '250px';
    cover.style.opacity = 0;
    this.style.color = ' rgba(255, 255, 255, 0.9)';
    inputSearch.addEventListener('mouseleave', aa)
    inputSearch.value = 'Search';


    //壁纸
    imgbg.style.filter = 'blur(0px)';
    imgbg.style.transform = 'scale(1)';

    //动态壁纸
    videobg.style.filter = 'blur(0px)';
    videobg.style.transform = 'scale(1)';
}
inputSearch.addEventListener('blur', function() {
    setTimeout(onblurs, 100);
});




var timeStart = function() {
    var date = new Date();
    time.innerHTML = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
}
timeStart();

setInterval(timeStart, 1000);